package com.guigu.www.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.guigu.www.pojo.*;
import com.guigu.www.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预约表 前端控制器
 * </p>
 *
 * @author wangyuhao
 * @since 2021-06-22
 */
@RestController
@CrossOrigin
public class TOrderController {
    @Autowired
    private TOrderService service;
    @Autowired
    private TOrdersettingService tOrdersettingService;
    @Autowired
    private TMemberExaminationService tMemberExaminationService;
    @Autowired
    private TCheckgroupCheckitemService tCheckgroupCheckitemService;
    @Autowired
    private TTjbgService tTjbgService;
    @PostMapping("tjyy")
    public boolean tjyy(@RequestBody TOrder tOrder) throws ParseException {
        tOrder.setOrderStatus("1");
        Boolean upadte = service.upadte(tOrder);
        TMemberExamination tMemberExamination=new TMemberExamination();
        tMemberExamination.setTSetmealId(tOrder.getSetmealId());
        tMemberExamination.setTMemberId(Integer.parseInt(tOrder.getMemberId()));
        tMemberExamination.setReportStatus(0);
        if(upadte){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(tOrder.getOrderDate());
            TOrdersetting tOrdersetting = tOrdersettingService.selectbyId(date);
            if(tOrdersetting==null){
                TOrdersetting tOrdersetting1=new TOrdersetting();
                tOrdersetting1.setOrderDate(date);
                tOrdersetting1.setNumber(9);
                tOrdersetting1.setReservations(1);
                tOrdersetting1.setCreatetime(new Date());
                tOrdersettingService.insert(tOrdersetting1);
                System.out.println(tMemberExamination);
                tMemberExaminationService.add(tMemberExamination);
                List<TCheckgroupCheckitem> checkitems = tCheckgroupCheckitemService.W_xgqz(tMemberExamination.getTSetmealId());//根据套餐id查询出来的检查项id
                for(TCheckgroupCheckitem t:checkitems){
                    TTjbg tTjbg=new TTjbg();
                    tTjbg.setTMemberExaminationId(tMemberExamination.getId());
                    tTjbg.setTCheckitemId(t.getCheckitemId());
                    tTjbgService.W_add(tTjbg);
                }
            }else{
                if(tOrdersetting.getNumber()<=10){
                    tOrdersetting.setNumber(tOrdersetting.getNumber()-1);
                    tOrdersetting.setReservations(tOrdersetting.getReservations()+1);
                    tOrdersettingService.updateById(tOrdersetting);
                }
            }
        }
        return upadte;
    }

    @PostMapping("yyshow")
    public int yyshow(@RequestBody TOrder tOrder) {
        TOrder selectone = service.selectone(tOrder);
        int a=0;
        if(selectone.getOrderStatus().equals("0")){
            a=0;
        }else{
            a=1;
        }
        return a;
    }
}

